package sms;
import java.util.Scanner;

/**
 * 需求:
 * 1. 能够通过键盘录入班级的总人数
 * 2. 能够依次录入每个学生的学号，姓名，语文成绩，数学成绩
 * 3. 能够展示所有的学生信息（上图）
 * 4. 能够展示语文最高和最低成绩的学生信息（上图）
 * 5. 能够展示数学最高和最低成绩的学生信息（上图）
 * 6. 能够展示全班语文成绩和数学成绩的平均分（上图）
 * 7. 能够按照学生总成绩从低到高显示学生信息（首列为名次）（上图）
 * 8. 能够根据学生的学号查询该学生信息（上图）
 */
public class studentmanagesystem {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入班级学生总人数:");
        int number = scan.nextInt();
        int[] snos = new int[number];
        String[] snames = new String[number];
        int[] chineseScores = new int[number];
        int[] mathScores = new int[number];
        int[] totalScore = new int[number];

        //2. 能够依次录入每个学生的学号，姓名，语文成绩，数学成绩
        addStudent(snos, snames, chineseScores, mathScores,totalScore);
        //3. 能够展示所有的学生信息（上图）
        printStudents(snos, snames, chineseScores, mathScores,totalScore);

       //4. 能够展示语文最高和最低成绩的学生信息
        // 5. 能够展示数学最高和最低成绩的学生信息
        int chineseMaxIndex = getMaxIndex(snos, chineseScores);
        int chineseMinIndex = getMinIndex(snos, chineseScores);
        int mathMaxIndex = getMaxIndex(snos, mathScores);
        int mathMinIndex = getMinIndex(snos, mathScores);

        System.out.println("--------------------语文最高和最低成绩---------------------");
        System.out.println("语文成绩最高的学生信息:");
        printMaxScore(chineseMaxIndex, snos, snames, chineseScores, mathScores, totalScore);
        System.out.println("语文成绩最低的学生信息:");
        printMinScore(chineseMinIndex, snos, snames, chineseScores, mathScores, totalScore);

        System.out.println("--------------------最高和最低成绩---------------------");
        System.out.println("数学成绩最高的学生信息:");
        printMaxScore(mathMaxIndex, snos, snames, chineseScores, mathScores, totalScore);
        System.out.println("数学成绩最低的学生信息:");
        printMinScore(mathMinIndex, snos, snames, chineseScores, mathScores, totalScore);

        //6. 能够展示全班语文成绩和数学成绩的平均分
        System.out.println("--------------------全班语文成绩和数学成绩的平均分--------------------");
       int chineseAvgScore = getAverageScore(snos, chineseScores);
        int mathTotalAvgScore = getAverageScore(snos, mathScores);
        System.out.println("全班语文成绩的平均分:" + chineseAvgScore);
        System.out.println("全班数学成绩的平均分:" + mathTotalAvgScore);

        //7. 能够按照学生总成绩从低到高显示学生信息（首列为名次）
        sortByTotalScores(snos, totalScore, snames, chineseScores, mathScores);



        System.out.println("--------------------班级排名--------------------");
        System.out.println("名次    学号    姓名    语文成绩    数学成绩    总成绩");
        for (int i = 0; i < snos.length; i++) {
            System.out.println((i + 1) + "    " + snos[i] + "    " + snames[i] + "    "
                    + chineseScores[i] + "    " + mathScores[i] + "    " + totalScore[i]);
        }
        //8. 能够根据学生的学号查询该学生信息
        System.out.println("--------------------根据学号查询学生信息--------------------");
        System.out.print("请输入要查询的学生学号:");
        int sno = scan.nextInt();
        int index = findBySno(snos, sno);
        if (index != 1) {
            System.out.println("--------------------查询结果--------------------");
            System.out.println("学号    姓名    语文成绩    数学成绩    总成绩");
            System.out.println(snos[index] + "    " + snames[index] + "    "
                    + chineseScores[index] + "    " + mathScores[index]
                    + "    " + totalScore[index]);
        } else {
            System.out.println("没有找到学号为" + sno + "的学生信息");
        }
    }

    public static int findBySno(int[] snos,int sno)  {
        int index = -1;//重要！！
        for (int i = 0; i < snos.length; i++) {
            if (sno == snos[i]) {
                index = i;
                break;
            }
        }
        return index;
    }
    public static void sortByTotalScores(int[] snos,int[] totalScore,String[] snames,int[] chineseScores,int[] mathScores) {
        for (int i = 0; i < snos.length - 1; i++) {
            for (int j = 0; j < snos.length - i - 1; j++) {
                if (totalScore[j] < totalScore[j + 1]) {
                    int temp = snos[j];
                    snos[j] = snos[j + 1];
                    snos[j + 1] = temp;

                    String tempStr = snames[j];
                    snames[j] = snames[j + 1];
                    snames[j + 1] = tempStr;

                    temp = chineseScores[j];
                    chineseScores[j] = chineseScores[j + 1];
                    chineseScores[j + 1] = temp;

                    temp = mathScores[j];
                    mathScores[j] = mathScores[j + 1];
                    mathScores[j + 1] = temp;

                    temp = totalScore[j];
                    totalScore[j] = totalScore[j + 1];
                    totalScore[j + 1] = temp;
                }
            }
        }
    }
    public static int getAverageScore(int[] snos,int[] score) {
        int sum = 0;
        for (int i = 0; i < snos.length; i++) {
            sum += score[i];
        }
        return sum / snos.length;
    }
    public static void printMinScore(int minIndex, int[] snos, String[] snames, int[] chineseScores, int[] mathScores, int[] totalScores) {
        System.out.println(snos[minIndex] + "  " + snames[minIndex] + "  "
                + chineseScores[minIndex] + "    " + mathScores[minIndex]
                + "    " + totalScores[minIndex]);
    }
    public static void printMaxScore(int maxIndex, int[] snos, String[] snames, int[] chineseScores, int[] mathScores, int[] totalScores) {
        System.out.println(snos[maxIndex] + "  " + snames[maxIndex] + "  "
                + chineseScores[maxIndex] + "    " + mathScores[maxIndex]
                + "    " + totalScores[maxIndex]);
    }
    public static int getMaxIndex(int[] snos,int[] scores) {
        int MaxIndex = 0;

        for (int i = 1; i < snos.length; i++) {
            if (scores[i] > scores[MaxIndex]) { //对比依然是成绩
                MaxIndex = i; //相当于擂台上存储索引
            }
        }
        return MaxIndex;
    }
    public static int getMinIndex(int[] snos,int[] scores) {
        int MinIndex = 0;

        for (int i = 1; i < snos.length; i++) {
            if (scores[i] < scores[MinIndex]) { //对比依然是成绩
                MinIndex = i; //相当于擂台上存储索引
            }
        }
        return MinIndex;
    }
    public static void addStudent(int[] snos, String[] snames, int[] chineseScores, int[] mathScores, int[] totalScore) {

        Scanner scan = new Scanner(System.in);

        for (int i = 0; i < snos.length; i++) {
            System.out.println("请输入第" + (i + 1) + "个学生的学号：");
            snos[i] = scan.nextInt();

            System.out.println("请输入第" + (i + 1) + "个学生的姓名：");
            snames[i] = scan.next();

            System.out.println("请输入第" + (i + 1) + "个学生的语文成绩：");
            chineseScores[i] = scan.nextInt();

            System.out.println("请输入第" + (i + 1) + "个学生的数学成绩：");
            mathScores[i] = scan.nextInt();

            System.out.println("已录入第" + (i + 1) + "个学生的成绩\n");
        }
        for (int i = 0; i < totalScore.length; i++) {
            totalScore[i] = chineseScores[i] + mathScores[i];
        }
    }

    public static void printStudents(int[] snos, String[] snames, int[] chineseScores, int[] mathScores, int[] totalScore) {
        System.out.println("-------------------学生信息----------------------");
        System.out.println("   学号    姓名    语文成绩     数学成绩     总成绩");
        for (int i = 0; i < snos.length; i++) {
            System.out.println(snos[i] + "   " + snames[i] + "   " + chineseScores[i]
                    + "    " + mathScores[i] + "    " + totalScore[i]);
        }
    }

}
